
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(use_native_unicode='utf8')

class user(db.Model):
    __tablename__ = 'users'
    __table_args__ = {'mysql_engine': 'InnoDB'}  # 支持事务操作和外键
    user_id=db.Column(db.String(16), doc="账户", primary_key=True)
    user_name=db.Column(db.String(10), doc='姓名', nullable=False)
    user_fileavl=db.Column(db.SmallInteger, doc='剩余翻译次数', default=3, nullable=False)
    user_pwd=db.Column(db.String(128), doc='密码', default='123456789', nullable=False)
    user_phone=db.Column(db.String(15), doc="手机号", nullable=False)
    user_mail=db.Column(db.String(30), doc="邮箱", nullable=False)
    login_time=db.Column(db.DateTime, doc='登录时间', nullable=False)
    local_ip = db.Column(db.String(15), doc='当前ip', nullable=False)
    files = db.relationship('file', backref='user', cascade='all', lazy='dynamic')


    def funct(self,session):
        session['user_id']=self.user_id
        session['user_name']=self.user_name
        session['user_fileavl']=self.user_fileavl
        session['user_pwd']=self.user_pwd
        session['user_phone']=self.user_phone
        session['mail']=self.user_mail
        session['login_time']=self.login_time
        session['local_ip']=self.local_ip
        return session
    # orders = db.relationship('Order', backref='users', cascade='all', lazy='dynamic')
    # coupons = db.relationship('Coupon', backref='users', cascade='all', lazy='dynamic')
    # favorites = db.relationship('Favorite', backref='users', cascade='all', lazy='dynamic')
    # comments = db.relationship('Comment', backref='users', cascade='all', lazy='dynamic')

class file(db.Model):#仅针对源文件建库 目标文件直接去output文件夹里去找
    __tablename__ = 'files'
    __table_args__ = {'mysql_engine': 'InnoDB'}  # 支持事务操作和外键
    file_name = db.Column(db.String(1024), doc="文件名", nullable=False)
    file_id = db.Column(db.Integer, doc="文件序号", primary_key=True, autoincrement=True)
    file_uptime=db.Column(db.DateTime, doc='上传时间', nullable=False)
    file_done=db.Column(db.Boolean, doc="是否翻译过", default=False)
    file_slen= db.Column(db.BigInteger, doc="源文件长度")
    file_dlen=db.Column(db.BigInteger, doc="译后长度")
    file_path = db.Column(db.String(128), doc="文件路径", default="3500x")
    file_format = db.Column(db.String(4), doc="文件格式", nullable=False, default="docx")
    user_id = db.Column(db.String(16), db.ForeignKey('users.user_id'))
    file_state = db.Column(db.Boolean,doc="翻译状态",default=False,nullable=False)#false未占用
